package com.wfh.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wfh.R;
import com.wfh.adapter.BannerAdapter;
import com.wfh.adapter.ProductAdapter;
import com.wfh.adapter.ShareAdapter;
import com.wfh.bean.BannerBean;
import com.wfh.bean.ProductBean;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ProductActivity extends CommonActivity {
	private ProductAdapter productadapter;
	private BroadcastReceiver mReceiver = new DataReceiver();
	private ViewPager viewPager;
	private MyDialog dialog;
	private TextView page;
	private List<ProductBean> products;
	private TextView product_content;
	private Button share;
	private Button join;
	private ListView listview;
	private Dialog alertDialog;
	private int pagePosition = 0;
	private TextView activity_title;
	private Drawable bg=null;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_product);

		// 初始化组件
		activity_title = (TextView) findViewById(R.id.activty_title);
		viewPager = (ViewPager) findViewById(R.id.viewpager1);
		page = (TextView) findViewById(R.id.page);
		dialog = MyDialog.create(this);
		product_content = (TextView) findViewById(R.id.product_content);
		share = (Button) findViewById(R.id.button1);
		join = (Button) findViewById(R.id.button2);
		
		RelativeLayout viewcon=(RelativeLayout)findViewById(R.id.viewpagercontainer);
		Drawable bg=getResources().getDrawable(R.drawable.banner_bg);
		viewcon.setBackgroundDrawable(bg);

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_PRODUCT);
		registerReceiver(mReceiver, filter);

		// 设置viewpager数字
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				page.setText(arg0 + 1 + "");
				product_content.setText(products.get(arg0).getProduct_desc());
				pagePosition = arg0;
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		getShare();// 初始化分享

		// 分享按钮
		share.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDialog.show();
			}
		});

		// 跳转分享

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle bd = new Bundle();
				bd.putString("content", products.get(pagePosition)
						.getProduct_name());
				bd.putString("imageurl", Util.getImageFullName(products.get(
						pagePosition).getProduct_img()));
				bd.putInt("type", position);
				Intent intent = new Intent();
				intent.putExtras(bd);
				intent.setClass(ProductActivity.this, ShareActivity.class);
				ProductActivity.this.startActivity(intent);
				alertDialog.hide();
			}
		});

		dialog.show();

	}

	@Override
	protected void onResume() {
		// 设置标题
		Map<String, Integer> atob = new HashMap<String, Integer>();
		atob.put("weeks", R.string.tejia);
		atob.put("hots", R.string.rexiao);
		atob.put("bests", R.string.newp);
		atob.put("promotions", R.string.xianshi);
		String action = getIntent().getStringExtra("action");
		activity_title.setText(atob.get(action));
		// 调用service读取数据
		HomeActivity.service.getLocalProducts(action, this, null);
		super.onResume();
	}

	// 接收器
	private class DataReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			products = bd.getParcelableArrayList("products");
			if (products.size() == 0 || products == null)
				return;

			if (productadapter != null) {
				productadapter.clearImgcache();
			}
			productadapter = new ProductAdapter(ProductActivity.this, products);
			viewPager.setAdapter(productadapter);
			productadapter.notifyDataSetChanged();
			product_content.setText(products.get(0).getProduct_desc());// 设置产品内容
			dialog.hide();
		}
	}

	// 创建dialog并设置监听器

	public void getShare() {
		listview = new ListView(this);
		listview.setAdapter(new ShareAdapter(this));
		alertDialog = Util.alertDialog(this, listview, "分享给大家", "");
	}

	@Override
	protected void onDestroy() {
		if (dialog != null)
			dialog.cancel();
		if (productadapter != null)
			productadapter.clearImgcache();
		if (alertDialog != null)
			alertDialog.cancel();
		if(bg!=null){
			bg.setCallback(null);
		}
		unregisterReceiver(mReceiver);
		super.onDestroy();
	}

}
